package br.hm.operandroid.activity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import br.hm.operandroid.R;
import br.hm.operandroid.manager.Configurator;
import br.hm.operandroid.manager.OperatorUtils;
import br.hm.telephoneoperator.CallOperatorException;
import br.hm.telephoneoperator.CallOperatorService;
import br.hm.telephoneoperator.Phone;
import br.hm.telephoneoperator.PhoneOperator;
import br.hm.telephoneoperator.PhonePortabilityDetail;
import br.hm.telephoneoperator.yoegi.impl.YoegiCallOperationService;
import br.hm.util.DateUtils;
import br.hm.util.PhoneUtils;

public class HistoryOperatorActivity extends Activity implements
		OnClickListener {

	private CallOperatorService callOperatorService;

	private ListView listView;
	private ImageButton findButton;
	private EditText textPhone;
	private PhonePortabilityDetailAdapter adapter;
	private Handler handler;

	public HistoryOperatorActivity() {
		callOperatorService = new YoegiCallOperationService();
		handler = new Handler();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		adapter = new PhonePortabilityDetailAdapter();

		setContentView(R.layout.layout_find_history_operator);

		listView = (ListView) findViewById(R.id.listHistory);
		listView.setAdapter(adapter);
		textPhone = (EditText) findViewById(R.id.historyTextPhone);
		findButton = (ImageButton) findViewById(R.id.btn_find);
		findButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {

		Phone phone;
		try {
			String dddDefault = Configurator
					.getDDDDefault(getApplicationContext());
			dddDefault = dddDefault == null ? "11" : dddDefault;
			phone = PhoneUtils.parser(textPhone.getText().toString(),
					dddDefault, "55");
			Thread thread = new ThreadPhonePortabilityDetails(phone, this);
			thread.start();
		} catch (ParseException e) {
			showToast("Telefone informado esta invalido");
		}
	}

	protected void showToast(final String result) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(HistoryOperatorActivity.this, result,
						Toast.LENGTH_LONG).show();
			}
		});

	}

	class ThreadPhonePortabilityDetails extends Thread {

		private ProgressDialog progressDialog;
		private Phone phone;

		public ThreadPhonePortabilityDetails(Phone phone, Context context) {
			this.phone = phone;
			progressDialog = ProgressDialog.show(context, "Operadora",
					"Buscando Operadora, favor aguardar...", false, true);
		}

		@Override
		public void run() {
			try {
				List<PhonePortabilityDetail> phonePortabilityDetails = callOperatorService
						.getHistoryPhonePortabilityDetail(phone);
				adapter.setListPhonePortabilityDetail(phonePortabilityDetails);
				handler.post(new Runnable() {
					@Override
					public void run() {
						adapter.notifyDataSetChanged();
					}
				});
			} catch (CallOperatorException e) {
				showToast("Problema ao solicitar dados da operadora");
			} finally {
				closeProgressDialog();
			}
		}

		protected void closeProgressDialog() {
			handler.post(new Runnable() {
				@Override
				public void run() {
					progressDialog.dismiss();
				}
			});
		}
	}

	class PhonePortabilityDetailAdapter extends BaseAdapter {

		private List<PhonePortabilityDetail> phonePortabilityDetails;

		public PhonePortabilityDetailAdapter() {
			this.phonePortabilityDetails = new ArrayList<PhonePortabilityDetail>();
		}

		public List<PhonePortabilityDetail> getListPhonePortabilityDetail() {
			return phonePortabilityDetails;
		}

		public void setListPhonePortabilityDetail(
				List<PhonePortabilityDetail> details) {
			this.phonePortabilityDetails = details;
		}

		@Override
		public int getCount() {
			return phonePortabilityDetails.size();
		}

		@Override
		public Object getItem(int index) {
			return phonePortabilityDetails.get(index);
		}

		@Override
		public long getItemId(int index) {
			return index;
		}

		@Override
		public View getView(int index, View view, ViewGroup viewGroup) {
			PhonePortabilityDetail portabilityDetail = phonePortabilityDetails
					.get(index);
			PhoneOperator phoneOperator = portabilityDetail.getPhoneOperator();
			View v = getLayoutInflater().inflate(R.layout.layout_row_operator,
					null);

			String name = phoneOperator.getName().toUpperCase();
			String corporateName = phoneOperator.getCorporateName()
					.toUpperCase();

			ImageView imagem = (ImageView) v.findViewById(R.id.historyImage);
			imagem.setImageResource(OperatorUtils.getImageResource(name));

			TextView simpleLabel = (TextView) v
					.findViewById(R.id.historySimpleLabel);
			simpleLabel.setText(name);

			TextView fullLabel = (TextView) v
					.findViewById(R.id.historyFullLabel);
			fullLabel.setText(corporateName);

			TextView dateLabel = (TextView) v
					.findViewById(R.id.historyDateLabel);
			Date datePortability = portabilityDetail.getDatePortability();
			if (datePortability == null) {
				dateLabel.setText("---");
			} else {
				dateLabel.setText(DateUtils
						.convertToStringDDMMYYYY_HHMM(datePortability));
			}

			return v;
		}

	}
}
